/*
Copyright (C) 2010-2011 Sylvain Martin (Spintec - CEA/CNRS/UJF/G-INP)
All rights reserved.

This file is part of the SpinToolBow

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "SPlot2DWidget.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	SPlot2DWidget::SPlot2DWidget(QWidget *parent)
///
/// \brief	Constructor.
///
/// \author	Sylvain Martin
/// \date	2/4/2011
///
/// \param [in,out]	parent	If non-null, the Qt parent.
////////////////////////////////////////////////////////////////////////////////////////////////////

SPlot2DWidget::SPlot2DWidget(QWidget *parent)
	:QwtPlot(parent), zoomer(NULL), flagZoom(false),
	 winScale(NULL){
		//on initialise le zoom
		// LeftButton for the zooming
		// MidButton for the panning
		// RightButton: zoom out by 1
		// Ctrl+RighButton: zoom out to full size
		zoomer = new SPlotZoomer(canvas());
		zoomer->setMaxStackDepth(200);
		zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
			Qt::RightButton, Qt::ControlModifier);

		zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
			Qt::RightButton);

		//permet de deplacer la courbe
		panner = new QwtPlotPanner(canvas());
		panner->setAxisEnabled(QwtPlot::yRight, false);
		panner->setMouseButton(Qt::MidButton);

		// Avoid jumping when labels with more/less digits
		// appear/disappear when scrolling vertically
		// ba je ne sais pas trop pourquoi...
		const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
		QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
		sd->setMinimumExtent( fm.width("100.0000") );

		const QColor c(Qt::darkBlue);
		zoomer->setRubberBandPen(c);
		zoomer->setTrackerPen(c);

		//on le deactive jusqu'a qq1 click sur le bouton
		zoomer->setEnabled(flagZoom);
		panner->setEnabled(flagZoom);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SPlot2DWidget::zoom(bool on)
///
/// \brief	(SLOT) actives or deactivates the zooms function.
///
///
///  LeftButton for the zooming
///  MidButton for the panning
///  RightButton: zoom out by 1
///  Ctrl+RighButton: zoom out to full size
////////////////////////////////////////////////////////////////////////////////////////////////////

void SPlot2DWidget::zoom(){
	flagZoom= !flagZoom;
	zoomer->setEnabled(flagZoom);
	panner->setEnabled(flagZoom);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SPlot2DWidget::autoScale(bool on)
///
/// \brief	(SLOT) Reset the scale with the autoScale.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

void SPlot2DWidget::autoScale(){
	this->setAxisAutoScale(0);
	this->setAxisAutoScale(2);
	zoomer->setZoomBase();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SPlot2DWidget::selectScale()
///
/// \brief	(SLOT) Open a dialog box asking the scale.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

void SPlot2DWidget::selectScale(){
	if(winScale==NULL){
		winScale = new SPlotSetScale(this);
	}

	winScale->initialValue(	this->axisScaleDiv(QwtPlot::xBottom)->lowerBound(),
							this->axisScaleDiv(QwtPlot::xBottom)->upperBound(),
							this->axisScaleDiv(QwtPlot::yLeft)->lowerBound(),
							this->axisScaleDiv(QwtPlot::yLeft)->upperBound());

	connect(winScale, SIGNAL(scaleChange(double,double,double,double)),
			this, SLOT(setScale(double,double,double,double)));

	winScale->show();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SPlot2DWidget::setScale(double Xmin, double Xmax, double Ymin, double Ymax)
///
/// \brief	(SLOT) sets the scales selected by the user.
///
/// \param Xmin the maximum of the x scale
/// \param Xmax the minimum of the x scale
/// \param Ymin the maximum of the y scale
/// \param Ymax the minimum of the y scale
////////////////////////////////////////////////////////////////////////////////////////////////////
void SPlot2DWidget::setScale(double Xmin, double Xmax, double Ymin, double Ymax){
	this->setAxisScale(QwtPlot::xBottom,Xmin, Xmax);
	this->setAxisScale(QwtPlot::yLeft, Ymin, Ymax);
	this->replot();
}

